import java.util.Scanner;

public class Vigenere {
	/**
	 * 大写字母表
	 */
	static String dateList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	/**
	 * 处理密钥
	 * 
	 * @param key
	 * @param inform
	 * @return
	 */
	public static String dealKey(String key, String inform) {
		key = key.toUpperCase();// 转大写
		key = key.replaceAll("[^A-Z]", "");// 去除所有非字母的字符

		StringBuffer sb = new StringBuffer(key);
		String realKey = "";
		if (sb.length() != inform.length()) {
			// 如果密钥长度与inform不同，则需要生成密钥字符串
			if (sb.length() < inform.length()) {
				// 如果密钥长度比inform短，则以不断重复密钥的方式生成密钥字符串
				while (sb.length() < inform.length()) {
					sb.append(key);
				}
			}
			// 此时，密钥字符串的长度大于或等于inform长度
			// 将密钥字符串截取为与inform等长的字符串
			realKey = sb.substring(0, inform.length());
		}
		return realKey;

	}
	/**
	 * 
	 * @param inform//原文
	 * @param key//密钥
	 * @return 密文
	 */
	public static String buildPass(String inform, String key) {
		inform = inform.toUpperCase();// 转大写
		inform = inform.replaceAll("[^A-Z]", "");// 去除所有非字母的字符

		key = dealKey(key, inform);
		int len = key.length();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < len; i++) {
			int row = dateList.indexOf(key.charAt(i));// 密码表的行

			int col = dateList.indexOf(inform.charAt(i));// 密码表的列号

			int index = (row + col) % 26;
			sb.append(dateList.charAt(index));// 生成密文

		}
		return sb.toString();
	}


	public static void main(String[] args) {
		Vigenere vigenere=new Vigenere();
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("输入原文：");
		
		String P = scanner.nextLine();

		System.out.println("输入密钥：");
		String K = scanner.nextLine();
		String C=vigenere.buildPass(P, K);
		System.out.println("密文：" + C);


	}
}
